from django.db import models

# Create your models here.
class SiteUser(models.Model):
    gender_choice=(
        (0,"未知"),
        (1,"男"),
        (2,"女"),
    )
    name=models.CharField(max_length=128,unique=True,verbose_name="用户名")
    password=models.CharField(max_length=256,verbose_name="密码")
    email=models.EmailField(unique=True,verbose_name="电子邮箱")
    #gender=models.CharField(choices=gender_choice,default=0,verbose_name="性别")
    gender=models.IntegerField(choices=gender_choice,default=0,verbose_name="性别")
    create_time=models.DateTimeField(auto_now_add=True,verbose_name="创建时间")
    modify_time=models.DateTimeField(auto_now=True,verbose_name="最后一次修改时间")
    last_login_time=models.DateTimeField(null=True,blank=True,verbose_name="最后一次登录时间")
    #必须邮箱验证成功后，才能登录成功
    has_confirmed=models.BooleanField(default=False,verbose_name="是否邮箱验证")
    def __str__(self): # 魔术方法，字符串的友好展示，便于调试
        return self.name

    class Meta: #内嵌类，给model定义元数据
       verbose_name="网站用户管理"
       verbose_name_plural=verbose_name
class ConfirmString(models.Model):
    code = models.CharField(max_length=256, verbose_name="确认码")
    user = models.OneToOneField('SiteUser', on_delete=models.CASCADE)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.user.name + ":" + self.code

    class Meta:
        ordering = ["-create_time"]
        verbose_name = "确认码"
        verbose_name_plural = "确认码"